from sqlalchemy import Column, Integer, String, Boolean, Text, ForeignKey, Date, DateTime
from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy import create_engine
from ..config import DATABASE_URL

# 创建数据库引擎
engine = create_engine(
    DATABASE_URL,
    connect_args={"check_same_thread": False} if "sqlite" in DATABASE_URL else {}
)

# 创建会话工厂
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# 创建基类
Base = declarative_base()

# 数据库依赖
def get_db() -> Session:
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

# 用户模型
class User(Base):
    __tablename__ = "users"
    
    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    password_hash = Column(String(255), nullable=False)

# 网站配置模型
class WebsiteConfig(Base):
    __tablename__ = "website_config"
    
    id = Column(Integer, primary_key=True, index=True)
    site_name = Column(String(255), default="")
    site_description = Column(String(500), default="")
    personal_intro = Column(Text, default="")
    icp_record = Column(String(100), default="")
    copyright_info = Column(String(255), default="")
    background_effect = Column(String(50), default="gradient")
    background_image_url = Column(String(255), default="")  # 存储自定义背景图片URL


# 联系方式模型
class Contact(Base):
    __tablename__ = "contacts"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    type = Column(String(50), nullable=False)
    url = Column(String(255), nullable=False)
    icon = Column(String(255), default="")
    status = Column(Boolean, default=True)

# 链接分类模型
class LinkCategory(Base):
    __tablename__ = "link_categories"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), unique=True, nullable=False)
    status = Column(Boolean, default=True)
    is_builtin = Column(Boolean, default=False)  # 是否为内置分类

# 网站链接模型
class Link(Base):
    __tablename__ = "links"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    category_id = Column(Integer, ForeignKey("link_categories.id"), nullable=True)
    url = Column(String(255), nullable=False)
    icon = Column(String(255), default="")
    description = Column(String(500), default="")
    status = Column(Boolean, default=True)

# 标签模型
class Tag(Base):
    __tablename__ = "tags"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), unique=True, nullable=False)
    status = Column(Boolean, default=True)

# 标题模型
class Title(Base):
    __tablename__ = "titles"
    
    id = Column(Integer, primary_key=True, index=True)
    content = Column(String(255), nullable=False)
    status = Column(Boolean, default=True)

# 搜索引擎模型
class SearchEngine(Base):
    __tablename__ = "search_engines"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)
    url = Column(String(255), nullable=False)
    icon = Column(String(255), default="")
    status = Column(Boolean, default=True)

# 静态网页模型
class StaticPage(Base):
    __tablename__ = "static_pages"
    
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False)  # 页面名称
    directory = Column(String(255), nullable=False, unique=True)  # 页面目录，唯一
    url = Column(String(255), default="")  # 访问链接
    description = Column(Text, default="")  # 页面描述
    status = Column(Boolean, default=True)  # 是否启用

# 访问量统计模型
class VisitStat(Base):
    __tablename__ = "visit_stats"
    
    id = Column(Integer, primary_key=True, index=True)
    visit_date = Column(Date, nullable=False)  # 访问日期
    visit_count = Column(Integer, default=0)  # 访问次数
    created_at = Column(DateTime, default=datetime.utcnow)  # 创建时间